﻿//-------------------------------------------------------------------------------------------
// <copyright file="Settings.cs" company="Russell Wilkins">
//     Copyright (c) Russell Wilkins 2014. All Rights Reserved.
// </copyright>
// <author>Russell Wilkins</author>
// <license href="license.txt">
//     The use of this software is governed by the Microsoft Public License
//     which is included with this distribution.
// </license>
//-------------------------------------------------------------------------------------------
namespace Ripple.TestClient
{
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;

    /// <summary>
    /// Class for accessing and storing application settings.
    /// </summary>
    public class Settings : ApplicationSettingsBase
    {
        private static Settings defaultInstance = (Settings)ApplicationSettingsBase.Synchronized(new Settings());
        private Window appWindow;

        /// <summary>
        /// Initialises a new instance of the Settings class.
        /// </summary>
        public Settings()
        {
            // Upgrade settings.
            if (!this.AppVersion.Equals(Assembly.GetExecutingAssembly().GetName().Version.ToString()))
            {
                this.Upgrade();
                this.AppVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
            }
        }

        public static Settings Default
        {
            get
            {
                return defaultInstance;
            }
        }

        [UserScopedSettingAttribute]
        [DefaultSettingValueAttribute("")]
        public string AccountId
        {
            get
            {
                return (string)this["AccountId"];
            }

            set
            {
                this["AccountId"] = value;
            }
        }

        [UserScopedSettingAttribute]
        [DefaultSettingValueAttribute("")]
        public string AppVersion
        {
            get
            {
                return (string)this["AppVersion"];
            }

            set
            {
                this["AppVersion"] = value;
            }
        }

        [UserScopedSetting]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
        [DefaultSettingValue("600")]
        public double Height
        {
            get
            {
                return (double)this["Height"];
            }

            set
            {
                this["Height"] = value;
            }
        }

        [UserScopedSetting]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
        [DefaultSettingValue("0")]
        public double Left
        {
            get
            {
                return (double)this["Left"];
            }

            set
            {
                this["Left"] = value;
            }
        }

        [UserScopedSetting]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
        [DefaultSettingValue("False")]
        public bool Maximized
        {
            get
            {
                return (bool)this["Maximized"];
            }

            set
            {
                this["Maximized"] = value;
            }
        }

        [UserScopedSetting]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
        [DefaultSettingValue("0")]
        public double Top
        {
            get
            {
                return (double)this["Top"];
            }

            set
            {
                this["Top"] = value;
            }
        }

        [UserScopedSetting]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
        [DefaultSettingValue("600")]
        public double Width
        {
            get
            {
                return (double)this["Width"];
            }

            set
            {
                this["Width"] = value;
            }
        }

        #region Main Application Window

        public void InitalizeApplicationWindow(Window appWindow)
        {
            this.appWindow = appWindow;
            this.appWindow.Closing += new System.ComponentModel.CancelEventHandler(this.AppWindow_Closing);

            this.appWindow.Top = this.Top;
            this.appWindow.Left = this.Left;
            this.appWindow.Height = this.Height;
            this.appWindow.Width = this.Width;
            if (this.Maximized)
            {
                this.appWindow.WindowState = WindowState.Maximized;
            }
        }

        private void AppWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (this.appWindow.WindowState == WindowState.Maximized)
            {
                this.Top = this.appWindow.RestoreBounds.Top;
                this.Left = this.appWindow.RestoreBounds.Left;
                this.Height = this.appWindow.RestoreBounds.Height;
                this.Width = this.appWindow.RestoreBounds.Width;
                this.Maximized = true;
            }
            else
            {
                this.Top = this.appWindow.Top;
                this.Left = this.appWindow.Left;
                this.Height = this.appWindow.Height;
                this.Width = this.appWindow.Width;
                this.Maximized = false;
            }

            this.Save();
        }

        #endregion Main Application Window
    }
}